package com.neusoft.controller;

import com.neusoft.core.CommonResult;
import com.neusoft.core.Food;
import com.neusoft.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RefreshScope
@RestController
@RequestMapping("/FoodController")
public class FoodController {

    @Autowired
    private FoodService foodService;
    @PostMapping("/Food")
    public CommonResult<List<Food>> listFoodByBusinessId(@RequestBody Food food) throws Exception{
        List<Food> list = foodService.listFoodByBusinessId(food.getBusinessId());
        return new CommonResult<>(200,"success(10200)",list);
    }
    @PostMapping("/OneFood")
    public CommonResult<Integer> saveFood(@RequestBody Map<String, Object> map) throws Exception {
        // 从 map 中获取参数
        String foodName = (String) map.get("foodName");
        String foodExplain = (String) map.get("foodExplain");
        String foodImg = (String) map.get("foodImg");
        // 这里使用 Number 来接收可能是 Integer 或 Double 的值
        Number foodPriceNumber = (Number) map.get("foodPrice");
        Double foodPrice = foodPriceNumber.doubleValue();  // 将其转换为 Double
        Integer businessId = (Integer) map.get("businessId");
        Integer quantity = (Integer) map.get("quantity");
        // 将参数传递给 service
        int result = foodService.saveFood(foodName, foodExplain, foodImg, foodPrice, businessId, quantity);
        return new CommonResult<>(200, "success(10200)", result);
    }
    //
    @PutMapping("/Food")
    public CommonResult<Integer> updateFood(@RequestBody Food food) throws Exception {
        if (food.getFoodName() == null || food.getFoodName().isEmpty()) {
            throw new IllegalArgumentException("食品名称不能为空");
        }
        int result = foodService.updateFood(food);
        return new CommonResult<>(200, "success(10200)", result);
    }

    @PutMapping("/orderFood")
    public void orderFood(@RequestBody Food food) throws Exception {
        foodService.orderFood(food.getFoodId(), food.getQuantity());
    }
    //
    @DeleteMapping("/Food")
    public CommonResult<Integer> removeFood(@RequestParam Integer foodId) throws Exception {
        int result = foodService.removeFood(foodId);
        return new CommonResult<>(200, "success(10200)", result);
    }
    //
    @GetMapping("/GetOneFood")
    public CommonResult<Food> getFoodById(@RequestParam Integer foodId) throws Exception {
        Food result = foodService.getFoodById(foodId);
        return new CommonResult<>(200, "success(10200)", result);
    }


}
